<?php

namespace app\api\controller\xykeep;

use addons\xykeep\service\Store as StoreService;
use app\api\model\xykeep\store\Commission as CommissionModel;
use app\common\controller\Api;
use app\common\model\Store as StoreModel;
use app\api\model\xykeep\User as UserModel;

use app\api\model\xykeep\goods\Order as GoodsOrderModel;
use app\api\model\xykeep\user\Withdraw as WithdrawModel;
class Store extends Api
{
    protected $noNeedLogin = [''];
    protected $noNeedRight = ['*'];

    /**
     * 商家钱包
     */
    public function info()
    {
        $user = $this->auth->getUserinfo();
        $store = new StoreService($user['id']);
        $storeInfo = $store->getStoreInfo();

        $this->success('分销商详情', $storeInfo);
    }

    /**
     * 商家钱包
     */
    public function money()
    {
        $user = $this->auth->getUserinfo();
        $store = new StoreService($user['id']);
        $storeInfo = $store->getStoreInfo();

        $this->success('分销商详情', $storeInfo);
    }

    /**
     * 账单(佣金明细)
     */
    public function bill()
    {
        $params = $this->request->post();
        $data = CommissionModel::getLists($params);
        $this->success('商家余额明细', $data);
    }


    //订单管理
    public function order_list()
    {
        $params = $this->request->param();

        $where = [];
        if(isset($params['store_id']) && $params['store_id']){
            $where['store_id'] = $params['store_id'];
        }else{
            $where['store_id'] = 0;
        }

        if (isset($params['date_filter']) && $params['date_filter'] !== '') {
            if($params['date_filter'] == 'today') {
                // 获取今天的开始和结束时间戳
                $todayStart = strtotime("today"); // 今天开始时间戳
                $todayEnd = strtotime("tomorrow") - 1; // 今天结束时间戳
                $where['createtime'] = [ 'between', [$todayStart, $todayEnd]];
            }elseif ($params['date_filter'] == 'yesterday') {
                // 获取昨天的开始和结束时间戳
                $yesterdayStart = strtotime("yesterday"); // 昨天开始时间戳
                $yesterdayEnd = strtotime("today")- 1; // 昨天结束时间戳
                $where['createtime'] = [ 'between', [$yesterdayStart, $yesterdayEnd]];
            }elseif ($params['date_filter'] == 'week') {
                // 获取本周的开始和结束时间戳
                $weekStart = strtotime("monday this week"); // 本周开始时间戳
                $weekEnd = strtotime("sunday this week 23:59:59"); // 本周结束时间戳
                $where['createtime'] = [ 'between', [$weekStart, $weekEnd]];
            }elseif ($params['date_filter'] == 'custom') {
                $where['createtime'] = [ 'between', [$params['custom_start'], $params['custom_end']]];
            }
        }

        if(isset($params['status']) && $params['status']!=='all'){
            if($params['status']=='3' || $params['status']=='5'){
                $where['status'] = ['in','3,5'];
            }else{
                $where['status'] = $params['status'];
            }
        }
        $data = GoodsOrderModel::where($where)->with('item')->order('id desc')->paginate(10);

        //订单数
        $order_count = GoodsOrderModel::where($where)->count();
        //成交额
        $order_capital = GoodsOrderModel::where($where)->sum('pay_fee');

        $this->success('',[
            'data'=>$data,
            'order_count' => $order_count,
            'order_capital' => $order_capital,
        ]);
    }

    //订单发货
    public function take_order()
    {
        $params = $this->request->param();
        $GoodsOrderModel = new GoodsOrderModel();
        $orderInfo = $GoodsOrderModel->where([
            'id' => $params['id'],
            'status' => 1
        ])->find();
        if(!$orderInfo){
            $this->error('订单不存在');
        }
        $orderInfo->save([
            'status' => 2,
            'delivery_time' => time()
        ]);
        $this->success('发货成功');
    }

}